package com.chris.easymallware.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chris.common.constant.WareConstant;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chris.common.utils.PageUtils;
import com.chris.common.utils.Query;

import com.chris.easymallware.dao.PurchaseDetailDao;
import com.chris.easymallware.entity.PurchaseDetailEntity;
import com.chris.easymallware.service.PurchaseDetailService;


@Service("purchaseDetailService")
public class PurchaseDetailServiceImpl extends ServiceImpl<PurchaseDetailDao, PurchaseDetailEntity> implements PurchaseDetailService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<PurchaseDetailEntity> pageWrapper = new Query<PurchaseDetailEntity>().getPageWrapper(params, PurchaseDetailEntity.class);
        LambdaQueryWrapper<PurchaseDetailEntity> lambda = pageWrapper.lambda();
        String status = (String) params.get("status");
        if (StringUtils.isNotBlank(status)) {
            lambda.eq(PurchaseDetailEntity::getStatus, status);
        }
        String wareId = (String) params.get("wareId");
        if (StringUtils.isNotBlank(wareId)) {
            lambda.eq(PurchaseDetailEntity::getWareId, wareId);
        }
        IPage<PurchaseDetailEntity> page = this.page(
                new Query<PurchaseDetailEntity>().getPage(params),
                pageWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public void updateInfoByPurchaseId(List<Long> ids) {

        List<PurchaseDetailEntity> purchaseDetailEntities = this.list(new LambdaQueryWrapper<PurchaseDetailEntity>()
                        .in(PurchaseDetailEntity::getPurchaseId, ids)
//                .in(PurchaseDetailEntity::getStatus, Collections.singletonList(WareConstant.PurchaseDetailStatusEnum.assigned.getCode()))
        )
                .stream()
                .peek(purchaseDetailEntity -> purchaseDetailEntity.setStatus(WareConstant.PurchaseDetailStatusEnum.buying.getCode()))
                .collect(Collectors.toList());
        this.updateBatchById(purchaseDetailEntities);

    }

}